package simple

func nextGreaterElement(nums1 []int, nums2 []int) []int {

	stack := make([]int, 0)
	numsMap := map[int]int{}
	for i := 0; i < len(nums2); i++ {
		for len(stack) > 0 && nums2[stack[len(stack)-1]] < nums2[i] {
			pop := stack[len(stack)-1]
			stack = stack[:len(stack)-1]
			numsMap[nums2[pop]] = nums2[i]
		}
		stack = append(stack, i)
	}

	for i := 0; i < len(nums1); i++ {
		v, ok := numsMap[nums1[i]]
		if !ok {
			nums1[i] = -1
			continue
		}
		nums1[i] = v
	}

	return nums1
}
